package com.swx.identityservice.service;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.swx.identityservice.models.ScbpPermission;
import com.swx.identityservice.models.ScbpUser;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.Transient;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * @author LC
 * @version V1.0.0
 * @date 2018-7-9 10:19
 */

@Data
@ApiModel(value = "登录用户名密码实体类")
public class LoginUser extends ScbpUser implements UserDetails {

    @NotBlank(message = "密码不能为空")
    @Size(max = 18, min = 6)
    @ApiModelProperty(value = "密码", example = "123456")
    private String password;

    private static final long serialVersionUID = -1379274258881257107L;

    private String token;
    /** 登陆时间戳（毫秒） */
    private Long loginTime;
    /** 过期时间戳 */
    private Long expireTime;

    @Transient
    private List<ScbpPermission> permissions = new ArrayList<>();

    // 账户是否未过期
    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    // 账户是否未锁定
    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    // 密码是否未过期
    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    // 账户是否激活
    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }


    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {

        List<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();

        for (Iterator<ScbpPermission> it = permissions.iterator(); it.hasNext(); ) {
            ScbpPermission p = it.next();
            if (!StringUtils.isEmpty(p.getPermission())) {
                auths.add(new SimpleGrantedAuthority(p.getPermission()));
            }
        }
        return auths;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public Long getLoginTime() {
        return loginTime;
    }

    public void setLoginTime(Long loginTime) {
        this.loginTime = loginTime;
    }

    public Long getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(Long expireTime) {
        this.expireTime = expireTime;
    }

    public List<ScbpPermission> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<ScbpPermission> permissions) {
        this.permissions = permissions;
    }
}


